package com.goods.business.service.imp;

import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductMapper productMapper;
    //分类树形
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {
        List<ProductCategoryVO> productCategoryVOList = findAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS =
                ProductCategoryConverter.converterToTreeNodeVO(productCategoryVOList);
        List<ProductCategoryTreeNodeVO> tree = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        List<ProductCategoryTreeNodeVO> page ;
        if (pageNum!=null&&pageSize!=pageSize){
            page = ListPageUtils.page(tree,pageSize, pageNum);
            return new PageVO<>(tree.size(),page);
        }else {
            return new PageVO<>(tree.size(),tree);
        }
    }

    //获取父类分级数据
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
    List<ProductCategoryVO> productCategoryVOList = findAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.converterToTreeNodeVO(productCategoryVOList);
        return CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
    }

    //添加
    @Override
    public ResponseBean add(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
       return ResponseBean.success();
    }

    //根据Id获取信息
    @Override
    public ProductCategoryVO edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = ProductCategoryConverter.converterToProductCategoryVO(productCategory);
        return productCategoryVO;
    }

    //修改
    @Override
    public void update(Long id, ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO , productCategory);
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    //删除商品类别
    @Override
    public void delete(Long id) throws BusinessException {
        //先查询是否有这条数据
        ProductCategory productCategory
                = productCategoryMapper.selectByPrimaryKey(id);
        //判断是否为空值
        if (productCategory==null){
            //等于空，直接返回
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该分类不存在");
        }else {
            //查询是否有子分类
            Example example = new Example(ProductCategory.class);
            example.createCriteria().andEqualTo("pid",id);
            int selectCountByExample = productCategoryMapper.selectCountByExample(example);
            //判断是否存在子类
            if (selectCountByExample!=0){
                //表示存在子类
                throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该分类存在子分类,不能直接删除");
            }else {
                //表示不存在子类
                //检查该分类是否有物资引用
                Example o1 = new Example(Product.class);
                o1.createCriteria().andEqualTo("oneCategoryId",id)
                        .orEqualTo("twoCategoryId",id)
                        .orEqualTo("threeCategoryId",id);
                if(productMapper.selectCountByExample(o1)!=0){
                    throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该分类存在物资引用,无法直接删除");
                }
                    productCategoryMapper.deleteByPrimaryKey(id);
                }
            }
    }

    //所有商品类别
    private List<ProductCategoryVO> findAll() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        return ProductCategoryConverter.converterToVOList(productCategories);
    }

}
